package org.cpoy.ei.session;

import org.cpoy.eims.entity.*;
import java.util.ArrayList;
import java.util.List;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;

@Name("teacherHome")
public class TeacherHome extends EntityHome<Teacher> {

	@In(create = true)
	UserAccountHome userAccountHome;
	@In(create = true)
	AddressHome addressHome;

	public void setTeacherId(Integer id) {
		setId(id);
	}

	public Integer getTeacherId() {
		return (Integer) getId();
	}

	@Override
	protected Teacher createInstance() {
		Teacher teacher = new Teacher();
		return teacher;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		UserAccount userAccount = userAccountHome.getDefinedInstance();
		if (userAccount != null) {
			getInstance().setUserAccount(userAccount);
		}
		Address addressByPresentAddressId = addressHome.getDefinedInstance();
		if (addressByPresentAddressId != null) {
			getInstance().setAddressByPresentAddressId(
					addressByPresentAddressId);
		}
		Address addressByPermanentAddressId = addressHome.getDefinedInstance();
		if (addressByPermanentAddressId != null) {
			getInstance().setAddressByPermanentAddressId(
					addressByPermanentAddressId);
		}
	}

	public boolean isWired() {
		if (getInstance().getUserAccount() == null)
			return false;
		if (getInstance().getAddressByPresentAddressId() == null)
			return false;
		return true;
	}

	public Teacher getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

	public List<Course> getCourses() {
		return getInstance() == null ? null : new ArrayList<Course>(
				getInstance().getCourses());
	}

}
